<?php
/**
 * File from WEBO Video Transmuter, WEBO Software (http://www.webogroup.com/)
 * Provides core admin interface.
 *
 **/
class webo_video_transmuter_admin {
/* constructor */
	function webo_video_transmuter_admin () {
		$this->basepath = realpath(dirname(__FILE__) . '/../') . '/';
		@include_once($this->basepath . 'controller/config.php');
		$this->config = new webo_video_transmuter_config();
		@include_once($this->basepath . 'controller/converter.php');
		$this->converter = new webo_video_transmuter_converter($this->config->options);
	}
/* get all movies */
		function getVideos() {
			$video = array();
			$videodir = $this->config->options['videodir'];
			if ($dir = @opendir($videodir)) {
				$relative = str_replace($this->config->options['rootdir'], "/", $videodir);
				while (($file = @readdir($dir)) !== false) {
					if (!@is_dir($videodir . $file) && !preg_match("!\.(state|desc)$!", $file)) {
						$name = preg_replace("!\.[^\.]+$!", "", $file);
						$thumb = 'thumb/' . $name . '.jpg';
						$thumb = @is_file($videodir . $thumb) ? $relative . $thumb : '';
						$state = @file_get_contents($videodir . $name . '.state');
						$description = @file_get_contents($videodir . $name . '.desc');
						$ogv = @is_file($videodir . 'theora/' . $name . '.ogv');
						$mp4 = @is_file($videodir . 'mp4/' . $name . '.mp4');
						$video[@filemtime($videodir . $file)] = array(
							'thumb' => $thumb,
							'state' => $state,
							'ogv' => $ogv,
							'mp4' => $mp4,
							'description' => $description,
							'name' => $name
						);
					}
				}
				krsort($video);
			}
			return $video;
		}
/* display UI */
	function display ($page = 'main') {
		$error = 0;
		if (!empty($_POST['wvt_submit'])) {
			if (!empty($_FILES['wvt_file']) || !empty($_POST['wvt_url']) || !empty($_POST['wvt_path'])) {
				if ($file = $this->converter->uploadVideo()) {
					if ($file == 'FILETYPE_NOT_SUPPORTED') {
						$error = 5;
					} else {
						if (!empty($this->config->options['ffmpeg'])) {
							$state_file = $this->config->options['videodir'] .
								preg_replace("!\.[^\.]+$!is", "" , $file) . '.state';
							@file_put_contents($state_file, 1);
							$this->converter->convertVideo($file);
							if (empty(@file_get_contents($state_file))) {
								$error = 4;
							}
							$wvt_local_upload = 1;
						} else {
							$wvt_service_upload = 1;
						}
						@file_put_contents($this->config->options['videodir'] .
							preg_replace("!\.[^\.]+$!is", "" , $file) . '.desc', $_POST['wvt_description']);
					}
				} else {
					if (!empty($_FILES['wvt_file'])) {
						$error = 1;
					} elseif (!empty($_POST['wvt_url'])) {
						$error = 2;
					} elseif (!empty($_POST['wvt_path'])) {
						$error = 3;
					}
				}
			}
			if (!empty($_POST['wvt_config']) && !$error) {
				$error = $this->config->updateOptions();
			}
		}
		$upload_max_filesize = @ini_get('upload_max_filesize');
		$post_max_size = @ini_get('post_max_size');
		$file_uploads = @ini_get('file_uploads');
		$page_variables = array(
			'wvt_service_upload' => empty($wvt_service_upload) ? 0 : 1,
			'wvt_local_upload' => empty($wvt_local_upload) ? 0 : 1,
			'wvt_file' => empty($file) ? '' : str_replace($this->config->options['rootdir'], '/', $this->config->options['videodir']) . $file,
			'wvt_rootdir' => $this->config->options['rootdir'],
			'wvt_videodir' => $this->config->options['videodir'],
			'wvt_host' => $this->config->options['host'],
			'wvt_api' => $this->config->options['api'],
			'wvt_ffmpeg' => $this->config->options['ffmpeg'],
			'wvt_lang' => $this->config->options['lang'],
			'wvt_email' => $this->config->options['email'],
			'wvt_convert_log' => $log,
			'wvt_converter' => str_replace($this->config->options['rootdir'], "/", $this->basepath) . 'convert.php',
			'wvt_video' => $this->getVideos(),
			'wvt_url' => $_POST['wvt_url'],
			'wvt_path' => $_POST['wvt_path'],
			'wvt_description' => $_POST['wvt_description'],
			'wvt_error' => $error,
			'wvt_file_uploads' => $file_uploads,
			'wvt_max_size' => $upload_max_filesize < $post_max_size ? $upload_max_filesize : $post_max_size,
			'wvt_curl' => @function_exists('curl_init')
		);
		include($this->basepath . 'view/' . $page . '.php');
	}
/* installation routine */
	function install ($root) {
		global $user_email;
		$language = preg_replace("/[^a-z]/", "", strtolower(preg_replace("/[-,;].*/", "", empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? 'en' : $_SERVER["HTTP_ACCEPT_LANGUAGE"])));
		$root = realpath($root);
		$this->config->setOption('videodir', $root . '/video/');
		$this->config->setOption('rootdir', realpath($_SERVER['DOCUMENT_ROOT']) . '/');
		$this->config->setOption('host', $_SERVER['HTTP_HOST']);
		$this->config->setOption('ffmpeg', @is_file('/usr/bin/ffmpeg') ? '/usr/bin/ffmpeg' : '');
		$this->config->setOption('lang', in_array($language, array('en', 'ru')) ? $language : 'en');
		$this->config->setOption('email', $user_email);
		$videodir = $this->config->options['videodir'];
		if (!@is_writable($videodir)) {
			@unlink($videodir);
			@mkdir($videodir, octdec('0775'));
		}
		@mkdir($videodir . 'thumb', octdec('0775'));
		@mkdir($videodir . 'mp4', octdec('0775'));
		@mkdir($videodir . 'theora', octdec('0775'));
		$this->config->saveOptions();
	}
}